import requests
import json
import time
from 获取单元信息 import headers

UPDATE_URL = "https://ad.xiaohongshu.com/api/leona/rtb/unit"

def construct_update_data(raw_data, new_intelligent_value=1):
    target_config = raw_data.get("targetConfig", {}).copy()
    target_config["intelligentExpansion"] = new_intelligent_value

    return {
        "sourceFlag": raw_data.get("sourceFlag", "web"),
        "unitId": raw_data.get("unitId"),
        "unitName": raw_data.get("unitName"),
        "eventBid": raw_data.get("eventBid"),
        "promotionTarget": raw_data.get("promotionTarget"),
        "targetConfig": target_config,
        "targetType": raw_data.get("targetType"),
        "keywordTargetPeriod": raw_data.get("keywordTargetPeriod"),
        "keywordTargetAction": raw_data.get("keywordTargetAction", []),
        "removeBind": raw_data.get("removeBind", 0),
        "noteIds": raw_data.get("noteIds", []),
        "pageEnterTime": raw_data.get("pageEnterTime", int(time.time() * 1000)),
        "pageSubmitTime": int(time.time() * 1000)
    }

def construct_update_device_price_data(raw_data, targetDevicePrice=None):
    target_config = raw_data.get("targetConfig", {}).copy()
    target_config["targetDevicePrice"] = targetDevicePrice

    return {
        "sourceFlag": raw_data.get("sourceFlag", "web"),
        "unitId": raw_data.get("unitId"),
        "unitName": raw_data.get("unitName"),
        "eventBid": raw_data.get("eventBid"),
        "promotionTarget": raw_data.get("promotionTarget"),
        "targetConfig": target_config,
        "targetType": raw_data.get("targetType"),
        "keywordTargetPeriod": raw_data.get("keywordTargetPeriod"),
        "keywordTargetAction": raw_data.get("keywordTargetAction", []),
        "removeBind": raw_data.get("removeBind", 0),
        "noteIds": raw_data.get("noteIds", []),
        "pageEnterTime": raw_data.get("pageEnterTime", int(time.time() * 1000)),
        "pageSubmitTime": int(time.time() * 1000)
    }

def construct_search_note_ids_data(raw_data):
    return {
        "keywordGenType": raw_data.get("keywordGenType", 0),
        "keywordWithBid": raw_data.get("keywordWithBid", []),
        "noteIds": raw_data.get("noteIds", []),
        "pageEnterTime": raw_data.get("pageEnterTime", int(time.time() * 1000)),
        "pageSubmitTime": int(time.time() * 1000),
        "phraseMatchTypeUpgrade": raw_data.get("phraseMatchTypeUpgrade", 0),
        "promotionTarget": raw_data.get("promotionTarget", 1),
        "removeBind": raw_data.get("removeBind", 0),
        "sourceFlag": raw_data.get("sourceFlag", "web"),
        "targetConfig": raw_data.get("targetConfig", {}),
        "targetPosition": raw_data.get("targetPosition", 1),
        "unitId": raw_data.get("unitId"),
        "unitName": raw_data.get("unitName"),
        "eventBid": raw_data.get("eventBid"),
    }

def update_intelligent_expansion(raw_data, new_intelligent_value=1, unit_dict_cookie=None, account_id=None):
    """
    提交修改后的数据
    """
    headers['v-seller-id'] = account_id
    update_data = construct_update_data(raw_data, new_intelligent_value)

    response = requests.put(
        UPDATE_URL,
        headers=headers,
        cookies=unit_dict_cookie,
        data=json.dumps(update_data, ensure_ascii=False)
    )

    return {
        "code": response.status_code,
        "text": response.text,
        "json": response.json() if response.status_code == 200 else {}
    }

def update_device_price(raw_data, device_price_str='0-1999#2000-3999#4000-5999#6000-7999#8000-100000', unit_dict_cookie=None, account_id=None):
    """
    提交修改后的数据
    """
    headers['v-seller-id'] = account_id
    update_data = construct_update_device_price_data(raw_data, device_price_str)

    response = requests.put(UPDATE_URL, headers=headers, cookies=unit_dict_cookie, data=json.dumps(update_data, ensure_ascii=False))

    return {
        "code": response.status_code,
        "text": response.text,
        "json": response.json() if response.status_code == 200 else {}
    }
